﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IntelligentDecisioning.SharePoint.Logging;
using Microsoft.SharePoint;

namespace IntelligentDecisioning.SharePoint.Helpers
{
    public class IDFeatureHelper: IDSharePointHelper
    {
        #region Constructor

        public IDFeatureHelper(SPSite siteCollection, SPWeb targetWeb)
            : base(siteCollection, targetWeb, new NoLog())
        {

        }

        public IDFeatureHelper(SPSite siteCollection, SPWeb targetWeb, ILog log)
            : base(siteCollection, targetWeb, log)
        {

        }

        #endregion

        #region Methods

        public void ActivateSiteFeature(Guid featureId)
        {
            try
            {
                Log.WriteToLog("Attempting to activate feature " + featureId.ToString());
                base.SiteCollection.Features.Add(featureId);
            }
            catch (Exception e)
            {
                Log.WriteToLog("Unexpected error activating feature", e);
            }
        }

        public void ActivateWebFeature(Guid featureId)
        {
            try
            {
                Log.WriteToLog("Attempting to activate feature " + featureId.ToString());
                base.TargetWeb.Features.Add(featureId);
            }
            catch (Exception e)
            {
                Log.WriteToLog("Unexpected error activating feature", e);
            }
        }

        public void DeactivateSiteFeature(Guid featureId)
        {
            try
            {
                Log.WriteToLog("Attempting to deactivate feature " + featureId.ToString());
                base.SiteCollection.Features.Remove(featureId);
            }
            catch (Exception e)
            {
                Log.WriteToLog("Unexpected error activating feature", e);
            }
        }

        public void DeactivateWebFeature(Guid featureId)
        {
            try
            {
                Log.WriteToLog("Attempting to deactivate feature " + featureId.ToString());
                base.TargetWeb.Features.Remove(featureId);
            }
            catch (Exception e)
            {
                Log.WriteToLog("Unexpected error activating feature", e);
            }
        }

        public void ProvisionSiteFeatures(List<Guid> featureIds)
        {
            foreach (Guid featureId in featureIds)
            {
                ActivateSiteFeature(featureId);
            }
        }

        public void ProvisionWebFeatures(List<Guid> featureIds)
        {
            foreach (Guid featureId in featureIds)
            {
                ActivateWebFeature(featureId);
            }
        }

        #endregion
    }
}
